import { useAuthStore } from '@/store/modules/auth';
import { localStg } from '@/utils/storage';
import { fetchRefreshToken } from '../api';
import type { RequestInstanceState } from './type';

export function getAuthorization() {
    const token = localStg.get('token');
    const Authorization = token ? `Bearer ${token}` : null;

    return Authorization;
}

export function getLanguage() {
    const language = localStg.get('lang');
    return language || null;
}

/** refresh token */
async function handleRefreshToken() {
    const { resetStore } = useAuthStore();

    const rToken = localStg.get('refreshToken') || '';
    const { error, data } = await fetchRefreshToken(rToken);
    if (!error) {
        localStg.set('token', data.authorization);
        localStg.set('refreshToken', data.refreshAuthorization);
        return true;
    }

    resetStore();

    return false;
}

export async function handleExpiredRequest(state: RequestInstanceState) {
    if (!state.refreshTokenFn) {
        state.refreshTokenFn = handleRefreshToken();
    }

    const success = await state.refreshTokenFn;

    setTimeout(() => {
        state.refreshTokenFn = null;
    }, 1000);

    return success;
}

export function showErrorMsg(state: RequestInstanceState, message: string) {
    if (!state.errMsgStack?.length) {
        state.errMsgStack = [];
    }

    const isExist = state.errMsgStack.includes(message);

    if (!isExist) {
        state.errMsgStack.push(message);

        window.$message?.error(message, {
            onLeave: () => {
                state.errMsgStack = state.errMsgStack.filter((msg) => msg !== message);

                setTimeout(() => {
                    state.errMsgStack = [];
                }, 5000);
            }
        });
    }
}
